<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>使用DocToc为Markdown文档自动生成目录 | 避海拦天的学习笔记</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="./favicon.ico">
    <meta name="description" content="BHLT's Blog">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="./assets/css/0.styles.4e303abf.css" as="style"><link rel="preload" href="./assets/js/app.eeeee38d.js" as="script"><link rel="preload" href="./assets/js/3.58d0d062.js" as="script"><link rel="preload" href="./assets/js/1.cb177be2.js" as="script"><link rel="preload" href="./assets/js/8.eccec34b.js" as="script"><link rel="prefetch" href="./assets/js/10.3c746082.js"><link rel="prefetch" href="./assets/js/11.d2d08f8b.js"><link rel="prefetch" href="./assets/js/12.a8ba3e88.js"><link rel="prefetch" href="./assets/js/13.feb89dc4.js"><link rel="prefetch" href="./assets/js/14.9c823496.js"><link rel="prefetch" href="./assets/js/4.f0d67def.js"><link rel="prefetch" href="./assets/js/5.66d75df3.js"><link rel="prefetch" href="./assets/js/6.4f9c83c7.js"><link rel="prefetch" href="./assets/js/7.d6c7b755.js"><link rel="prefetch" href="./assets/js/9.bbf5ffc8.js">
    <link rel="stylesheet" href="./assets/css/0.styles.4e303abf.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-1aefc0b4><div data-v-1aefc0b4><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-1aefc0b4 data-v-1aefc0b4><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-25ba6db2 data-v-1aefc0b4 data-v-1aefc0b4><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>避海拦天的学习笔记</h3> <p class="description" data-v-25ba6db2 data-v-25ba6db2>BHLT's Blog</p> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>避海拦天</span>
            
          <span data-v-25ba6db2>2022 - </span>
          2023
        </a></span></div></div> <div class="hide" data-v-1aefc0b4><header class="navbar" data-v-1aefc0b4><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/./" class="home-link router-link-active"><img src="./bhlt.png" alt="避海拦天的学习笔记" class="logo"> <span class="site-name">避海拦天的学习笔记</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/./" class="nav-link"><i class="iconfont reco-home"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/./categories/实用工具/" class="nav-link"><i class="undefined"></i>
  实用工具
</a></li><li class="dropdown-item"><!----> <a href="/./categories/网站搭建/" class="nav-link"><i class="undefined"></i>
  网站搭建
</a></li><li class="dropdown-item"><!----> <a href="/./categories/随笔/" class="nav-link"><i class="undefined"></i>
  随笔
</a></li></ul></div></div><div class="nav-item"><a href="/./tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/./timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间轴
</a></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-1aefc0b4></div> <aside class="sidebar" data-v-1aefc0b4><div class="personal-info-wrapper" data-v-39576ba9 data-v-1aefc0b4><img src="./avatar.png" alt="author-avatar" class="personal-img" data-v-39576ba9> <h3 class="name" data-v-39576ba9>
    避海拦天
  </h3> <div class="num" data-v-39576ba9><div data-v-39576ba9><h3 data-v-39576ba9>4</h3> <h6 data-v-39576ba9>文章</h6></div> <div data-v-39576ba9><h3 data-v-39576ba9>8</h3> <h6 data-v-39576ba9>标签</h6></div></div> <ul class="social-links" data-v-39576ba9></ul> <hr data-v-39576ba9></div> <nav class="nav-links"><div class="nav-item"><a href="/./" class="nav-link"><i class="iconfont reco-home"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/./categories/实用工具/" class="nav-link"><i class="undefined"></i>
  实用工具
</a></li><li class="dropdown-item"><!----> <a href="/./categories/网站搭建/" class="nav-link"><i class="undefined"></i>
  网站搭建
</a></li><li class="dropdown-item"><!----> <a href="/./categories/随笔/" class="nav-link"><i class="undefined"></i>
  随笔
</a></li></ul></div></div><div class="nav-item"><a href="/./tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/./timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间轴
</a></div> <!----></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-25ba6db2 data-v-1aefc0b4><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>使用DocToc为Markdown文档自动生成目录</h3> <!----> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>避海拦天</span>
            
          <span data-v-25ba6db2>2022 - </span>
          2023
        </a></span></div></div> <div data-v-1aefc0b4><main class="page"><section><div class="page-title"><h1 class="title">使用DocToc为Markdown文档自动生成目录</h1> <div data-v-f875f3fc><i class="iconfont reco-account" data-v-f875f3fc><span data-v-f875f3fc>避海拦天</span></i> <i class="iconfont reco-date" data-v-f875f3fc><span data-v-f875f3fc>2022/4/19</span></i> <i class="iconfont reco-eye" data-v-f875f3fc><span id="./blogs/shi-yong-gong-ju/2022/doctocsheng-cheng-mu-lu.html" data-flag-title="Your Article Title" class="leancloud-visitors" data-v-f875f3fc><a class="leancloud-visitors-count" style="font-size:.9rem;font-weight:normal;color:#999;"></a></span></i> <i class="tags iconfont reco-tag" data-v-f875f3fc><span class="tag-item" data-v-f875f3fc>Markdown</span><span class="tag-item" data-v-f875f3fc>DocToc</span></i></div></div> <div class="theme-reco-content content__default"><h2 id="_1-doctoc安装"><a href="#_1-doctoc安装" class="header-anchor">#</a> 1. DocToc安装</h2> <p>使用npm进行安装</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token function">npm</span> <span class="token function">install</span> -g doctoc
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="_2-用法"><a href="#_2-用法" class="header-anchor">#</a> 2. 用法</h2> <h3 id="_2-1-为单个文件生成目录"><a href="#_2-1-为单个文件生成目录" class="header-anchor">#</a> 2.1 为单个文件生成目录</h3> <p>直接在文件所在目录运行命令 <code>doctoc</code>+<code>文件名</code></p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code>doctoc example.md
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>效果：会在文档的开头自动生成目录。</p> <p><img src="./assets/img/image-20220419102111420.39efea3b.png" alt="image-20220419102111420"></p> <p>需要更新目录的话，直接再次运行命令即可。</p> <h3 id="_2-2-自定义生成目录的位置"><a href="#_2-2-自定义生成目录的位置" class="header-anchor">#</a> 2.2 自定义生成目录的位置</h3> <p>自动生成的目录默认位置在文档的开头，如果想自定义设置目录的位置，在喜欢的位置插入下面两行注释，再运行生成目录的命令，会自动定位在该位置生成目录。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&lt;!-- START doctoc --&gt;
&lt;!-- END doctoc --&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>例如，我希望在前言后插入目录，则在该位置插入两行注释，然后运行<code>doctoc example.md</code><img src="./assets/img/image-20220419103829262.4b5852ea.png" alt="image-20220419103829262"></p> <p>效果：</p> <p><img src="./assets/img/image-20220419104106904.c23d003a.png" alt="image-20220419104106904"></p> <h3 id="_2-3-自定义生成目录的标题级别"><a href="#_2-3-自定义生成目录的标题级别" class="header-anchor">#</a> 2.3 自定义生成目录的标题级别</h3> <p>默认情况下，会将所有的标题都生成在目录中，如果标题级别很多，有时候会显得很杂乱。</p> <p>可以自定义想要生成目录的标题级别，在运行命令时使用<code>--maxlevel</code>参数。</p> <p>例如在上面的例子中，我只想要生成到二级标题即可，则运行命令</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code> doctoc --maxlevel <span class="token number">2</span> example.md
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>效果：</p> <p><img src="./assets/img/image-20220419104522566.1677dab8.png" alt="image-20220419104522566"></p> <h3 id="_2-4-自定义生成目录的标题名称"><a href="#_2-4-自定义生成目录的标题名称" class="header-anchor">#</a> 2.4 自定义生成目录的标题名称</h3> <p>可以自定义目录的标题名称，在生成时使用参数<code>--title</code>，例如</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code>doctoc --title <span class="token string">'**奇奇怪怪目录**'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><img src="./assets/img/image-20220419104951357.d0318b24.png" alt="image-20220419104951357"></p> <p>以后更新目录时，会自动使用以前设置过的标题名称，不需要再使用<code>--title</code>参数。</p> <h3 id="_2-5-为目录下所有markdown文档生成目录"><a href="#_2-5-为目录下所有markdown文档生成目录" class="header-anchor">#</a> 2.5 为目录下所有markdown文档生成目录</h3> <p>在目录下运行命令，会为该目录和所有子目录中的markdown文档生成目录。</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code>doctoc <span class="token builtin class-name">.</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>如果有些文档不想生成目录，则在该文档的头部添加注释，DocToc会自动忽略这些文档。</p> <div class="language-markdown line-numbers-mode"><pre class="language-markdown"><code><span class="token comment">&lt;!-- DOCTOC SKIP --&gt;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="_2-6-为目录下所有已有目录的文档更新目录"><a href="#_2-6-为目录下所有已有目录的文档更新目录" class="header-anchor">#</a> 2.6 为目录下所有已有目录的文档更新目录</h3> <p>如果想要更新一个目录下已经自动生成过目录的所有文档，保持没有目录的文档不变，则使用命令</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code>doctoc --update-only <span class="token builtin class-name">.</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>或</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>doctoc -u .
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="参考资料"><a href="#参考资料" class="header-anchor">#</a> 参考资料</h2> <ol><li><a href="https://github.com/thlorenz/doctoc" target="_blank" rel="noopener noreferrer">DocToc GitHub<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ol></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">最后更新: </span> <span class="time">2022/4/19 13:24:40</span></div></footer> <!----> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-cb1513f6><li class="level-2" data-v-cb1513f6><a href="/./blogs/shi-yong-gong-ju/2022/doctocsheng-cheng-mu-lu.html#_1-doctoc安装" class="sidebar-link reco-side-_1-doctoc安装" data-v-cb1513f6>1. DocToc安装</a></li><li class="level-2" data-v-cb1513f6><a href="/./blogs/shi-yong-gong-ju/2022/doctocsheng-cheng-mu-lu.html#_2-用法" class="sidebar-link reco-side-_2-用法" data-v-cb1513f6>2. 用法</a></li><li class="level-3" data-v-cb1513f6><a href="/./blogs/shi-yong-gong-ju/2022/doctocsheng-cheng-mu-lu.html#_2-1-为单个文件生成目录" class="sidebar-link reco-side-_2-1-为单个文件生成目录" data-v-cb1513f6>2.1 为单个文件生成目录</a></li><li class="level-3" data-v-cb1513f6><a href="/./blogs/shi-yong-gong-ju/2022/doctocsheng-cheng-mu-lu.html#_2-2-自定义生成目录的位置" class="sidebar-link reco-side-_2-2-自定义生成目录的位置" data-v-cb1513f6>2.2 自定义生成目录的位置</a></li><li class="level-3" data-v-cb1513f6><a href="/./blogs/shi-yong-gong-ju/2022/doctocsheng-cheng-mu-lu.html#_2-3-自定义生成目录的标题级别" class="sidebar-link reco-side-_2-3-自定义生成目录的标题级别" data-v-cb1513f6>2.3 自定义生成目录的标题级别</a></li><li class="level-3" data-v-cb1513f6><a href="/./blogs/shi-yong-gong-ju/2022/doctocsheng-cheng-mu-lu.html#_2-4-自定义生成目录的标题名称" class="sidebar-link reco-side-_2-4-自定义生成目录的标题名称" data-v-cb1513f6>2.4 自定义生成目录的标题名称</a></li><li class="level-3" data-v-cb1513f6><a href="/./blogs/shi-yong-gong-ju/2022/doctocsheng-cheng-mu-lu.html#_2-5-为目录下所有markdown文档生成目录" class="sidebar-link reco-side-_2-5-为目录下所有markdown文档生成目录" data-v-cb1513f6>2.5 为目录下所有markdown文档生成目录</a></li><li class="level-3" data-v-cb1513f6><a href="/./blogs/shi-yong-gong-ju/2022/doctocsheng-cheng-mu-lu.html#_2-6-为目录下所有已有目录的文档更新目录" class="sidebar-link reco-side-_2-6-为目录下所有已有目录的文档更新目录" data-v-cb1513f6>2.6 为目录下所有已有目录的文档更新目录</a></li><li class="level-2" data-v-cb1513f6><a href="/./blogs/shi-yong-gong-ju/2022/doctocsheng-cheng-mu-lu.html#参考资料" class="sidebar-link reco-side-参考资料" data-v-cb1513f6>参考资料</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div></div></div>
    <script src="./assets/js/app.eeeee38d.js" defer></script><script src="./assets/js/3.58d0d062.js" defer></script><script src="./assets/js/1.cb177be2.js" defer></script><script src="./assets/js/8.eccec34b.js" defer></script>
  </body>
</html>
